﻿Imports System.Data
Imports System.Configuration
Imports System.Collections.Generic
Imports System.Data.SqlClient
Imports TepcelDominio
Imports TepcelDAL.DBManager
Public Class EmpresaDAL : Inherits DBManager
    Public Function NuevoEmpresa(ByVal empresa As Empresa) As String
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("SPInsertEmpresa")
        comando.Parameters.Add("@codigoEmpresa", SqlDbType.Char, 3).Value = empresa.codigoEmpresa
        comando.Parameters.Add("@nombreEmpresa", SqlDbType.VarChar, 100).Value = empresa.nombreEmpresa
        comando.Parameters.Add("@rucEmpresa", SqlDbType.Char, 11).Value = empresa.rucEmpresa
        comando.Parameters("@codigoEmpresa").Direction = ParameterDirection.InputOutput

        MyBase.ExecuteQuery(comando)
        empresa.codigoEmpresa = comando.Parameters("@codigoEmpresa").Value

        Return empresa.codigoEmpresa

    End Function

    Public Function GetAllFromEmpresa() As List(Of Empresa)
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("GetAllFromEmpresa")
        Return SelectObjectFactory(comando)
    End Function

    Public Function SearchFromEmpresa(ByVal texto As String) As List(Of Empresa)
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("searchEmpresa")
        comando.Parameters.Add("@texto", SqlDbType.VarChar, 200).Value = texto
        Return SelectObjectFactory(comando)
    End Function

    Public Function DeleteEmpresa(ByVal codigoEmpresa As String) As Boolean
        Dim correcto As Boolean = False
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("DeleteEmpresa")
        comando.Parameters.Add("@codigoEmpresa", SqlDbType.Char, 3).Value = codigoEmpresa

        If MyBase.ExecuteQuery(comando) > 0 Then
            correcto = True
        End If

        Return correcto
    End Function


    Private Function SelectObjectFactory(ByVal comando As SqlCommand) As List(Of Empresa)
        Dim Lista As New List(Of Empresa)
        Using reader As SqlDataReader = MyBase.ExecuteReader(comando)
            While reader.Read
                Dim Empresa As New Empresa(reader.GetString(0), reader.GetString(1), reader.GetString(2))
                Lista.Add(Empresa)
                Empresa = Nothing
            End While
        End Using
        Return Lista
    End Function


End Class
